package com.ruoyi.api.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.product.dto.PageQuery;
import com.ruoyi.product.dto.ProductClassifyQueryDTO;
import com.ruoyi.product.dto.ProductDetailQueryDTO;
import com.ruoyi.product.dto.ProductQueryDTO;
import com.ruoyi.product.service.IProductClassifyService;
import com.ruoyi.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * 产品分类Controller
 *
 * @author ruoyi
 * @date 2020-12-27
 */
@RestController
@RequestMapping("/api/product")
@Api(tags = "商品接口")
public class ApiProductController extends ApiBaseController {

    @Autowired
    private IProductClassifyService productClassifyService;

    @Autowired
    private IProductService productService;

    @GetMapping("/classify/list")
    @ApiOperation("获取所有分类列表")
    public AjaxResult getTreeList() {
        return productClassifyService.getTreeList();
    }

    @GetMapping("/product/list")
    @ApiOperation("通过商品分类获取商品列表")
    public AjaxResult getProductsByClassify (@Valid ProductClassifyQueryDTO queryDTO){
        Long userId = getApiLoginMember();
        return productService.getProductsByClassify(userId, queryDTO);
    }

    @GetMapping("/product/detail")
    @ApiOperation("获取商品详情")
    public AjaxResult getProductsDetail (@Valid ProductDetailQueryDTO queryDTO){
        Long userId = getApiLoginMember();
        return productService.getProductsDetail(queryDTO, userId);
    }

    @GetMapping("/product/search")
    @ApiOperation("通过搜索商品名获取商品列表")
    public AjaxResult searchProductList (@Valid ProductQueryDTO queryDTO){
        Long userId = getApiLoginMember();
        return productService.searchProductList(queryDTO, userId);
    }

    @GetMapping("/product/relation")
    @ApiOperation("查询相关商品列表(当前商品同级或子级分类下的商品列表)")
    @ApiImplicitParam(name = "productId", value = "商品id" ,required = true)
    public AjaxResult searchRelationProductList (Long productId,@Valid PageQuery pageQuery) {
        Long userId = getApiLoginMember();
        return productService.searchRelationProductList(productId, pageQuery,userId);
    }

}
